package com.cpi.basilos.warehouse.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;

import com.cpi.basilos.corntype.converter.CornTypeConverter;
import com.cpi.basilos.corntype.dao.CornTypeDAO;
import com.cpi.basilos.corntype.dto.CornTypeDTO;
import com.cpi.basilos.corntype.exception.CornTypeException;
import com.cpi.basilos.size.dao.SizeDAO;
import com.cpi.basilos.size.exception.SizeException;
import com.cpi.basilos.stock.converter.StockConverter;
import com.cpi.basilos.stock.dao.StockDAO;
import com.cpi.basilos.stock.dto.StockDTO;
import com.cpi.basilos.stock.exception.StockException;
import com.cpi.basilos.stock.view.StockView;
import com.cpi.basilos.warehouse.converter.WarehouseConverter;
import com.cpi.basilos.warehouse.dao.WarehouseDAO;
import com.cpi.basilos.warehouse.dto.WarehouseDTO;
import com.cpi.basilos.warehouse.exception.WarehouseException;
import com.cpi.basilos.warehouse.view.WarehouseByLocationView;
import com.cpi.basilos.warehouse.view.WarehouseView;
import com.cs.common.exception.CSException;

public class WarehouseService {
	private static org.apache.log4j.Logger logger = Logger.getLogger(WarehouseService.class);
	public WarehouseView getStockByCornType(int warehouseId)throws WarehouseException{
		WarehouseView warehouseView = null;
		StockView stockView = null;
		List stockViews = null;
		
		WarehouseConverter warehouseConverter = null;
		StockConverter stockConverter = null;
		
		WarehouseDAO warehouseDAO = null;
		StockDAO stockDAO = null;
		CornTypeDAO cornTypeDAO = null;
		SizeDAO sizeDAO = null;
		
		WarehouseDTO warehouseDTO = null;
		StockDTO stockDTO = null;
		CornTypeDTO cornTypeDTO = null;
		
		List cornTypesByWarehouse = null;
		Iterator cornTypesIt = null;
		double sapStock = 0;
		double size = 0;
		double totalStock = 0;
		try{
			warehouseConverter = new WarehouseConverter();
			stockConverter = new StockConverter();
			warehouseDAO = new WarehouseDAO();
			stockDAO = new StockDAO();
			cornTypeDAO = new CornTypeDAO();
			sizeDAO = new SizeDAO();
			warehouseDTO = warehouseDAO.getWarehouse(warehouseId);
			cornTypesByWarehouse = stockDAO.corntypesWithStock(warehouseId);
			cornTypesIt = cornTypesByWarehouse.iterator();
			stockViews = new ArrayList(cornTypesByWarehouse.size());
			while(cornTypesIt.hasNext()){
				stockDTO = (StockDTO)cornTypesIt.next();
				totalStock += stockDTO.getAmount();
				
				cornTypeDTO = cornTypeDAO.cornTypeList(stockDTO.getCornType().getCornTypeId());
				size = sizeDAO.totalSize(warehouseId, stockDTO.getCornType().getCornTypeId(), warehouseDTO.isLocation());
				
				// TODO Ejecutar en ambientes de CPI
				//sapStock = stockDAO.totalStockByCornTypeSAP(cornTypeDTO.getBaanCode(), warehouseDTO.getCompany(), warehouseDTO.getCode());
				
				stockDTO.setCornType(cornTypeDTO);
				stockDTO.setSapAmount(sapStock);
				stockDTO.setSize(size);
				
				stockView = stockConverter.convertDTOToView(stockDTO);
				stockViews.add(stockView);
			}
			warehouseDTO.setTotalStock(totalStock);
			warehouseView = warehouseConverter.convertDTOToView(warehouseDTO);
			warehouseView.setStockViews(stockViews);
		}catch(WarehouseException warehouseException){
			throw warehouseException;
		}catch(CornTypeException cornTypeException){
			WarehouseException warehouseException = null;
			warehouseException = new WarehouseException(cornTypeException, CSException.LAYER_SERVICE, CSException.ACTION_SELECT);
			throw warehouseException;
		}catch(StockException stockException){
			WarehouseException warehouseException = null;
			warehouseException = new WarehouseException(stockException, CSException.LAYER_SERVICE, CSException.ACTION_SELECT);
			throw warehouseException;
		}catch(Exception exception){
			WarehouseException warehouseException = null;
			exception.printStackTrace(System.out);
			logger.error(exception);
			warehouseException = new WarehouseException(exception, CSException.LAYER_SERVICE, CSException.ACTION_SELECT);
			throw warehouseException;
		}
		return warehouseView;
	}
	/**
	 * 
	 * Servicio para obtener el tipo de 
	 * 
	 * @param locationId
	 * @param cornTypeId
	 * @return
	 * @throws WarehouseException
	 */
	public WarehouseByLocationView inventoryByUbication(int warehouseId, int cornTypeId) throws WarehouseException{
		WarehouseByLocationView warehouseByLocationView = new WarehouseByLocationView();
		CornTypeDAO cornTypeDAO = new CornTypeDAO();
		WarehouseDAO warehouseDAO = new WarehouseDAO();
		StockDAO stockDAO = new StockDAO();
		SizeDAO sizeDAO = null;
		WarehouseDTO warehouseDTO = null;
		List stockViewsList = new ArrayList(0);
		try {
			sizeDAO = new SizeDAO();
			CornTypeConverter cConverter = new CornTypeConverter();
			warehouseByLocationView.setCornTypeView(cConverter.convertDTOToView(cornTypeDAO.cornTypeList(cornTypeId)));
			WarehouseConverter wConverter = new WarehouseConverter();
			warehouseDTO = warehouseDAO.getWarehouse(warehouseId);
			warehouseByLocationView.setWarehouseView(wConverter.convertDTOToView(warehouseDTO));
			Iterator iterator = stockDAO.getStockList(cornTypeId, warehouseId).iterator();
			StockConverter sConverter = new StockConverter();
			while(iterator.hasNext()){
				StockDTO dto = (StockDTO) iterator.next();
				dto.setSize(sizeDAO.totalSizeByLocation(warehouseId, cornTypeId, warehouseDTO.isLocation(), dto.getLocation()));
				stockViewsList.add(sConverter.convertDTOToView(dto));
			}
			warehouseByLocationView.setStockViewsList(stockViewsList);
		}catch(WarehouseException warehouseException){
			throw warehouseException;
		}catch(SizeException sizeException){
			WarehouseException warehouseException = null;
			warehouseException = new WarehouseException(sizeException, CSException.LAYER_SERVICE, CSException.ACTION_SELECT);
			throw warehouseException;
		}catch(CornTypeException cornTypeException){
			WarehouseException warehouseException = null;
			warehouseException = new WarehouseException(cornTypeException, CSException.LAYER_SERVICE, CSException.ACTION_SELECT);
			throw warehouseException;	
		} catch (StockException stockException) {
			WarehouseException warehouseException = null;
			warehouseException = new WarehouseException(stockException, CSException.LAYER_SERVICE, CSException.ACTION_SELECT);
			throw warehouseException;
		}
		
		return warehouseByLocationView;
	}
	
}